<?php ob_start(); 
  session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
	$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); 
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return ($theValue);
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"))
$bool=verifLibelle1('Code_direction') && verifLibelle('Nom_direction');
//var_dump($bool);
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")  && $bool && !existeChamp($database_microassur, $microassur,'direction','Code_direction',@$_POST['Code_direction'])) {


  $insertSQL = sprintf("INSERT INTO direction (Identifiant_direction, Code_direction, Nom_direction) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['Identifiant_direction'], "int"),
                       GetSQLValueString($_POST['Code_direction'], "text"),
                       GetSQLValueString($_POST['Nom_direction'], "text"));

  mysql_select_db($database_microassur, $microassur);
  
  mysql_query("START TRANSACTION");

  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
  
if($Result1) 
{ mysql_query("commit");header("location:index.php?ajout=success"); 
}
else { mysql_query("rollback");$message=colorer("Contacter la DSI, erreur création direction","false");}

}
elseif(isset($_POST["MM_insert"])&& !$bool) 
{
if(empty($_POST['Code_direction'])) $message=colorer("Vous devez saisir le code","false");
elseif(!verifLibelle1('Code_direction')) $message=colorer("Le code saisi comporte des caractères spéciaux interdits","false");

elseif(empty($_POST['Nom_direction'])) $message=colorer("Vous devez saisir le nom ","false");
elseif(!verifLibelle('Nom_direction')) $message=colorer("Le nom saisi comporte des caractères spéciaux interdits ","false");

else //$message=colorer("Erreur: Au moins un champ est vide ou valeur incorrecte","false");
	$message="";
}
elseif(isset($_POST["MM_insert"]) && existeChamp($database_microassur, $microassur,'direction','Code_direction',@$_POST['Code_direction']))$message=colorer("Cette direction existe déjà","false");
mysql_select_db($database_microassur, $microassur);
$query_rs = "SELECT * FROM direction";
$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);
?>
<?php  if(isset($_GET['ajout']) && $_GET['ajout']='success') $message=colorer("La direction est créée","true"); ?>
<?php if(isset($_GET['suppdirection']) && $_GET['suppdirection']=='err') $message=colorer("Contacter la DSI, erreur suppression direction","false"); ?>
<?php if(isset($_GET['moddirection']) && $_GET['moddirection']=='success') $message=colorer("La modification de la direction est prise en compte ","true"); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Direction";
					$nommodule2="Liste des directions";
					$nom_module_saisie="Ecran saisie";
					?>
			</div>
			<div class="cleared"></div>
		<div class="corps1">
			<!--/***************************************/-->
			
			<div class="imgleft"></div>
			<div class="imgmiddle">
			<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
			<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
			<div class="cleared"></div>
                <table class="tab_list bordh">
					<tr id="niv1">
						  <td class="col1">CODE</td>
						  <td class="col2">NOM</td>
					</tr>
                </table>
				<div class="content_table">
					<table class="tab_list">
							<?php $i=1; do 	{ ?>
							  <?php if ($totalRows_rs > 0) { // Show if recordset not empty ?>
							  <tr <?php if($i%2==0) echo 'class="colorligne"'; ?> >
								<td class="col1"><a href="moddirection.php?Identifiant_direction=<?php echo $row_rs['Identifiant_direction']; ?>"><?php echo $row_rs['Code_direction']; ?></a></td>
								<td class="col2"><a href="moddirection.php?Identifiant_direction=<?php echo $row_rs['Identifiant_direction']; ?>"><?php echo $row_rs['Nom_direction']; ?></a></td>
							  </tr>
								<?php } // Show if recordset not empty ?>

							  <?php $i++; } while ($row_rs = mysql_fetch_assoc($rs));?>
					</table>
				</div>
			</div>
			<div class="imgright"></div>
		<div class="cleared"></div>
		<!--/***************************************/-->
		<div id="br"></div>
		
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
			<div class="imgleft"></div>
			<div class="imgmiddle">
				<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
				<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nom_module_saisie; ?></h5></span><span class="btnright"></span></div>
					<div class="formulaire">
							<table class="tabform">
								<tr>
								  <td>CODE</td>
									<td><input class="resize_1" type="text" maxlength="5" name="Code_direction" value="<?php if(isset($_POST['Code_direction']))echo remind_text('Code_direction');?>" size="32" /></td>
								</tr>
								<tr>
								  <td>NOM</td>
								  <td><input name="Nom_direction" type="text"  maxlength="30" value="<?php if(isset($_POST['Nom_direction']))echo remind_text('Nom_direction');?>" size="40" /></td>
								</tr>
							</table>
					</div>
			</div>
			<div class="imgright"></div>				
				<div class="cleared"></div>
				
					<div class="bt1"><a href="<?php echo $retour; ?>"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
					<div class="bt"><span class="btleft"></span><input type="submit" name="Creer" value="Créer" class="resizeall btmiddle"/><span class="btright"></span></div>
					
				<div class="cleared"></div>
  <input type="hidden" name="Identifiant_direction" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
</div>
</body>
</html><?php ob_end_flush();?>
<?php
mysql_free_result($rs);
?>